package com.scm.controller;

import com.scm.pojo.*;
import com.scm.service.*;

import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName StorageController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/15 9:11
 * @Version 1.0
 **/
@RestController
public class StorageController {

    @Autowired
    private PoMainService poMainService;
    @Autowired
    private Util util;

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private SoMainService soMainService;

    @Autowired
    private ProductService productService;

    @Autowired
    private StockService stockService;

    @Autowired
    private ScmService scmService;
    //可入库订单
    @RequestMapping("getPutStorage")
    public QueryResult getPutStorage(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        int total =poMainService.getCountStoragePoMain();
        List<PoMain> storagePoMain = poMainService.getStoragePoMain(map);
        List<PoMain> show = util.show(storagePoMain);
        return QueryResult.ok(total,show);
    }
    //入库
    @RequestMapping("putStorage")
    public Result putStorage(String poID){
        factoryService.putStorage(poID);
        return Result.ok(null);
    }
    //可出库订单
    @RequestMapping("getStorageSoMain")
    public QueryResult getStorageSoMain(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        int total = soMainService.getCountStorageSoMain();
        List<SoMain> list = soMainService.getStorageSoMain(map);
        List<SoMain> list1 = util.showSoMain(list);
        return QueryResult.ok(total,list1);
    }
    @RequestMapping("outStorage")
    public Result outStorage(String soID,HttpServletRequest request){
        String account = (String) request.getSession().getAttribute("account");
        factoryService.outStorage(soID,account);
        return Result.ok(null);
    }
    //获取产品对应库存记录
    @RequestMapping("getStock")
    public Result getStock(String productCode){
        List<Stock> stock = stockService.getStock(productCode);
        for (Stock stock1:stock){
            stock1.setCreateUser(scmService.getUserName(stock1.getCreateUser()));
            if (stock1.getStockType().equals("1")){
                stock1.setStockType("入库");
            }else {
                stock1.setStockType("出库");
            }
        }
        return Result.ok(stock);

    }

    @RequestMapping("selectProduct")
    public Result selectProduct(String productCode,String name,String min,String max){
        List<Product> list = productService.selectProduct(productCode, name);
        if (max.isEmpty()){
            return Result.ok(list);
        }else {
            List<Product> collect = list.stream().filter(product -> Integer.parseInt(product.getNum()) > Integer.parseInt(min) && Integer.parseInt(product.getNum()) < Integer.parseInt(max)).collect(Collectors.toList());
            return Result.ok(collect);
        }
    }

    @RequestMapping("getOneProduct")
    public Result getOneProduct(String productCode){
        Product product = productService.getProduct(productCode);
        return Result.ok(product);
    }
    @RequestMapping("addCheckStock")
    public Result addCheckStock(String num, CheckStock checkStock, HttpServletRequest request){
        String account = (String) request.getSession().getAttribute("account");
        checkStock.setCreateUser(account);
        factoryService.addCheckStock(num,checkStock);
        return Result.ok(null);
    }
    @RequestMapping("getCheckStock")
    public Result getCheckStock(String productCode){
        List<CheckStock> list = stockService.getCheckStock(productCode);
        for (CheckStock checkStock:list){
            checkStock.setCreateUser(scmService.getUserName(checkStock.getCreateUser()));
        }
        return Result.ok(list);
    }
}
